package Day14;

import jdk.nashorn.internal.scripts.JO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class ChangePwdPanel extends JPanel implements ActionListener {
    private ATM05 atm05;
    private JLabel pwdChangeLabel = new JLabel("密码：");
    private JLabel pwdChange1Label = new JLabel("确认密码：");
    private JPasswordField passwordField = new JPasswordField();
    private JPasswordField passwordField1 = new JPasswordField();
    private Font font = new Font("宋体", Font.BOLD, 20);
    private JButton sureBtn = new JButton(new ImageIcon("Image/buttonSure.jpg"));
    private JButton cancelBtn = new JButton(new ImageIcon("Image/buttonOff.jpg"));

    public ChangePwdPanel(ATM05 atm05){
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        pwdChangeLabel.setBounds(300, 200, 80, 50);
        pwdChangeLabel.setFont(font);
        this.add(pwdChangeLabel);

        pwdChange1Label.setBounds(300, 250, 80, 50);
        pwdChange1Label.setFont(font);
        this.add(pwdChange1Label);

        passwordField.setBounds(380, 200, 250, 50);
        passwordField.setFont(font);
        this.add(passwordField);

        passwordField1.setBounds(380, 250, 250, 50);
        passwordField1.setFont(font);
        this.add(passwordField1);

        sureBtn.setBounds(370, 320, 140, 50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        cancelBtn.setBounds(520, 320, 140, 50);
        cancelBtn.addActionListener(this);
        this.add(cancelBtn);


    }
    @Override
    public void actionPerformed(ActionEvent e) {
        ArrayList<User> users = atm05.getUsers();
        User loginUser = atm05.getLoginUser();
        String pwd = String.valueOf(passwordField.getPassword());
        String pwd1 = String.valueOf(passwordField1.getPassword());
        if(e.getSource() == sureBtn){
            if(pwd.equals(pwd1)){
                JOptionPane.showMessageDialog(this,"修改密码成功,请重新登录");
                atm05.getLoginUser().setPwd(pwd1);
                fileUtil.writeObject(users,atm05.getFILE_PATH());
                atm05.showPanel("login");
                passwordField.setText("");
                passwordField1.setText("");
            }else if(pwd.equals(loginUser.getPwd()) || pwd1.equals(loginUser.getPwd())){
                JOptionPane.showMessageDialog(this,"不能输入旧密码，请重新输入");
            }else {
                JOptionPane.showMessageDialog(this,"两次密码输入不一致，请重新输入");
                atm05.showPanel("change");
            }
        }else if (e.getSource() == cancelBtn){
            atm05.showPanel("menu");
        }
    }
}
